Quick Start Typescript ~ 4์žฅ ์ •๋ฆฌ

์นดํƒˆ๋กœ๊ทธ
  1. 1. ๐Ÿ“p39. package.json
    1. 1.1. package.json ์ž‘์„ฑ
      1. 1.1.1. name
      2. 1.1.2. version
      3. 1.1.3. scripts
      4. 1.1.4. dependencies
      5. 1.1.5. devDependencies
  2. 2. ๐Ÿ“p40. npm ์ฃผ์š” ๋ช…๋ น์–ด
  3. 3. ๐Ÿ“p48. tsconfig.json ์„ค์ •
  4. 4. ๐Ÿ“p76. ๋ณ€์ˆ˜ ์„ ์–ธ
    1. 4.1. var vs let, const ์ฐจ์ด์ 
      1. 4.1.1. ํ˜ธ์ด์ŠคํŒ… ์ฐจ์ด
        1. 4.1.1.1. var์˜ ๊ฒฝ์šฐ
        2. 4.1.1.2. const, let์˜ ๊ฒฝ์šฐ
      2. 4.1.2. ํ•จ์ˆ˜ ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„(var) vs ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„(const, let)
        1. 4.1.2.1. var์˜ ๊ฒฝ์šฐ
        2. 4.1.2.2. const, let์˜ ๊ฒฝ์šฐ
  5. 5. ๐Ÿ“p86. ํƒ€์ž… ๊ณ„์ธต๋„
    1. 5.1. any
      1. 5.1.1. primitive type
      2. 5.1.2. Object
      3. 5.1.3. Union
      4. 5.1.4. intersection
      5. 5.1.5. Primitive type
        1. 5.1.5.1. (์ƒ๋žต) string, number, boolean
        2. 5.1.5.2. symbol
        3. 5.1.5.3. type
        4. 5.1.5.4. enum
      6. 5.1.6. Object Type
        1. 5.1.6.1. Array
          1. 5.1.6.1.1. array type
          2. 5.1.6.1.2. generic array type <>
        2. 5.1.6.2. Tuple
        3. 5.1.6.3. (์ƒ๋žต) Function
        4. 5.1.6.4. (์ƒ๋žต) ์ƒ์„ฑ์ž
        5. 5.1.6.5. (์ƒ๋žต) Class
        6. 5.1.6.6. (์ƒ๋žต) Interface
  6. 6. ๐Ÿ“p114 undefined !== null
  7. 7. ๐Ÿ“p123 for โ€ฆ in ๋ฌธ ์ฃผ์˜์‚ฌํ•ญ

๐Ÿ“– Quick Start Typescript ์„ ์ฝ๊ณ , ๊ฐ„๋‹จํžˆ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„์ด๋‚˜ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

Typescript ํ•™์Šตํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์ดํ•ด๋„๊ฐ€ ๋†’์ง€ ๋ชปํ•ด, ์ž‘์„ฑ์‹œ ์ฐธ๊ณ ํ•œ ๋งํฌ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“p39. package.json

ํ”„๋ก ํŠธ์—”๋“œ์—์„œ ๊ฐ€์žฅ ๋จผ์ € ์‚ดํŽด๋ณด๋Š” ํŒŒ์ผ

  1. package.json

  2. ๋ฒˆ๋“ค๋Ÿฌ: webpack.config.js or vue.config.js

    • JS ๋กœ๋”: tsconfig.json, babel.config.js or .babelrc

    • CSS ๋กœ๋”: postcss.config.js

  3. index.html - JS/CSS/Font ์‚ฝ์ž… ๋ณด๊ธฐ

  4. ๊ทธ์™ธ Lint(์ฝ”๋”ฉ ์ปจ๋ฒค์…˜): eslintrc.js

package.json ์ž‘์„ฑ

๊ฐœ์ธ์ ์œผ๋กœ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์ด๋‚˜ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„๋งŒ ์ •๋ฆฌ

์ด ์‚ฌ์ดํŠธ (๊ณต์‹)์—์„œ ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด์žˆ๋‹ค.

+ํ•œ๊ธ€ ์‚ฌ์ดํŠธ

name

์ฃผ์˜์‚ฌํ•ญ: name์—๋Š” ๋Œ€๋ฌธ์ž๋ฅผ ํฌํ•จํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.

version

๋ฒ„์ „ ๊ด€๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ Rule์ด ์กด์žฌ

๋ชจ๋ฅด๊ณ  ์žˆ๋˜ ๋ถ€๋ถ„

Semantic Versioning

scripts

๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ€์žฅ package.json ํŒŒ์ผ์„ ์—ด์—ˆ์„ ๋•Œ, ๊ฐ€์žฅ ๋จผ์ € ๋ณด๋Š” ํ•ญ๋ชฉ

์ฐธ๊ณ ๋งํฌ

npm-scripts

npm-run-scripts

๊ฐœ๋ฐœ์ž๊ฐ€ ์„ค์ •ํ•ด๋‘” ์ปค๋งจ๋“œ ๋ผ์ธ ๋ช…๋ น์–ด๋ฅผ alias์ฒ˜๋Ÿผ npm run <key๊ฐ’>์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
// package.json

"scripts": {
"test:make": "mkdir make-test-folder"
}
1
2
3
npm run test:make

# mkdir make-test-folder ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋˜๊ณ , ํ˜„์žฌ ๊ฒฝ๋กœ์— make-test-folder ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์ƒ๊ธด๋‹ค.

dependencies

์‹ค์ œ ๋ฐฐํฌ๋  ๋•Œ ํฌํ•จ๋˜๋Š” ํŒจํ‚ค์ง€๋“ค

npm install or npm install --save ๋กœ ์ €์žฅ๋œ ํŒจํ‚ค์ง€๋“ค

devDependencies

๊ฐœ๋ฐœ์šฉ์œผ๋กœ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€๋“ค (lint, test, bundle etc)

npm install -D or npm install --save-dev๋กœ ์ €์žฅ๋œ ํŒจํ‚ค์ง€๋“ค

๐Ÿ“p40. npm ์ฃผ์š” ๋ช…๋ น์–ด

npm ์ฃผ์š” ๋ช…๋ น์–ด๋ฅผ ์งง๊ฒŒ ์น  ์ˆ˜ ์žˆ๋‹ค. (๊ธฐ๋ณธ aliases)

์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด๋ผ์„œ ์งง๊ฒŒ ์น˜๋ฉด ํŽธํ•˜๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ์ด์ „
npm install <ํŒจํ‚ค์ง€๋ช…>
# ์ถ•์•ฝ
npm i <ํŒจํ‚ค์ง€๋ช…>

# ์—ฌ๋Ÿฌ ์ถ•์•ฝ

## ๊ธ€๋กœ๋ฒŒ ์„ค์น˜
npm i -g <ํŒจํ‚ค์ง€๋ช…>

## devDependency ์„ค์น˜
npm i -D <ํŒจํ‚ค์ง€๋ช…>

## ์‚ญ์ œ
npm rm <ํŒจํ‚ค์ง€๋ช…>

๊ทธ ์™ธ ๋ช…๋ น์–ด๋“ค

๐Ÿ“p48. tsconfig.json ์„ค์ •

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ ์˜ต์…˜ ์ •์˜๋œ ํŒŒ์ผ

๊ณต์‹ ํ™ˆํŽ˜์ด์ง€

์ฐธ๊ณ : ๊ฐ„๋‹จํžˆ ์ •๋ฆฌ๋œ ๋ธ”๋กœ๊ทธ

removeComments์˜ ๊ฒฝ์šฐ ์ฃผ์„์„ ์ œ๊ฑฐํ•˜์—ฌ ์ปดํŒŒ์ผ ํ•ด์ฃผ์ง€๋งŒ, ์ฃผ์„์„ ์ด์šฉํ•˜์—ฌ webpack์—์„œ ๋ฒˆ๋“ค๋งํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๋Š” ์ ์„ ์œ ์˜

ex. /router/index.ts

1
const DashboardView = () => import(/* webpackChunkName: "dashboard" */ '@/views/admin/dashboard/DashBoardView.vue');

์œ„์™€ ๊ฐ™์ด URL ๋ผ์šฐํŒ…๋ณ„๋กœ, code splitํ•˜์—ฌ ํŠน์ • JS๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ฃผ์„์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

*ํ•ด๋‹น ๋ผ์šฐํŒ…์œผ๋กœ ๋“ค์–ด๊ฐ”์„ ๋•Œ, dashboard.js ํŒŒ์ผ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๐Ÿ“p76. ๋ณ€์ˆ˜ ์„ ์–ธ

๊ธฐ์กด JS(ES5) var ๊ณผ ES6์—์„œ ์ƒ๊ธด let, const ์ฐจ์ด์ ์ด ์ค‘์š”

ํ˜„์žฌ๋Š” var์€ ์ž˜ ์‚ฌ์šฉํ•˜์ง€์•Š๋Š” ํŽธ, ์‹ค์ œ๋กœ ESLint์—์„œ๋„ no-var option์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ ๋ชปํ•˜๊ฒŒ ์„ค์ •๋จ

var vs let, const ์ฐจ์ด์ 

  1. ํ˜ธ์ด์ŠคํŒ…
  2. ํ•จ์ˆ˜ ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„(var) vs ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„(const, let)

ํ˜ธ์ด์ŠคํŒ… ์ฐจ์ด

์„ ์–ธ์ด ์œ ํšจ๋ฒ”์œ„์—์„œ ์ตœ์ƒ๋‹จ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ

JS์—์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ ํ˜ธ์ด์ŠคํŒ…(๋Œ์–ด์˜ฌ๋ฆผ)์œผ๋กœ ๋˜๋Š” ๊ฒƒ์ด function๊ณผ var์ด ์žˆ๋‹ค. ์•ˆ๋˜๋Š” ๊ฒƒ์œผ๋กœ๋Š” class, const, let์ด ์žˆ๋‹ค.

var์˜ ๊ฒฝ์šฐ
1
2
3
4
5
6
7
8
9
10
// ๊ฐœ๋ฐœ ์ž‘์„ฑ ์ฝ”๋“œ
var test = `global`; // ์ด๋ ‡๊ฒŒ ํ• ๋‹น๋œ ๋ณ€์ˆ˜๋Š” window.test ๊ฐ์ฒด๋กœ ํ• ๋‹น๋œ๋‹ค.
console.log(window.test); // `global`

func();
function func() {
console.log(test); // (A) ์—ฌ๊ธฐ ํ‘œ์‹œ๊ฐ’?
var test = `local`;
console.log(test); // (B) ์—ฌ๊ธฐ ํ‘œ์‹œ๊ฐ’?
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// ์‹ค์ œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ
var test;
test = `global`;

console.log(window.test); // `global`

function func() { // ํ•จ์ˆ˜๋„ ํ˜ธ์ด์ŠคํŒ… ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
var test;
console.log(test); // (A) undefined
test = `local`;
console.log(test); // (B) `local`
}

func();

(A) ๋ถ€๋ถ„์—์„œ func๋‚ด์—์„œ ์ง€์—ญ๋ณ€์ˆ˜๋กœ test ๋ณ€์ˆ˜๊ฐ€ ํ˜ธ์ด์ŠคํŒ… ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธ€๋กœ๋ฒŒ ๋ณ€์ˆ˜ test๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

const, let์˜ ๊ฒฝ์šฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const testConst = `global-const`;
let testLet = `global-let`;
console.log(window.testConst); // undefined const
console.log(window.testLet); // undefined let var๊ณผ ๋‹ค๋ฅด๊ฒŒ window ๊ฐ์ฒด์— ํ• ๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค.

// func(); // Error, ์•„์ง ์„ ์–ธ๋˜์ง€ ์•Š์Œ

const func = () => {
// console.log(testConst); // Error, ์•„์ง ์„ ์–ธ๋˜์ง€ ์•Š์Œ
// console.log(testLet); // Error, ์•„์ง ์„ ์–ธ๋˜์ง€ ์•Š์Œ
const testConst = `local-const`;
let testLet = `local-let`;
console.log(testConst); // `local-const`
console.log(testLet); // `local-let`
}

func();

ํ•จ์ˆ˜ ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„(var) vs ๋ธ”๋ก ๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„(const, let)

var์˜ ๊ฒฝ์šฐ
1
2
3
4
5
6
7
8
func();

function func() { // ๋ณ€์ˆ˜ ๋ฒ”์œ„ = ํ•จ์ˆ˜ ์•ˆ
if (true) {
var test = `any`;
}
console.log(test); // `any`
}
const, let์˜ ๊ฒฝ์šฐ
1
2
3
4
5
6
7
8
9
10
func();

function func() {
if (true) { // ๋ณ€์ˆ˜ ๋ฒ”์œ„ = {} ๋ธ”๋ก ์•ˆ
const testConst = `const`;
let testLet = `let`;
}
console.log(testConst); // Error, not defined
console.log(testLet); // Error, not defined
}

๐Ÿ“p86. ํƒ€์ž… ๊ณ„์ธต๋„

์ถœ์ฒ˜: Quick start Typescript p.86

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๋งํฌ

ํƒ€์ž… ์ฒดํฌ๊ฐ€ Javascript์—์„œ ์—†๋˜ ๋ถ€๋ถ„์ด๋ผ ์•„์ง ๋งŽ์ด ๋ถ€์กฑํ•œ ๋ถ€๋ถ„

any

๊ฐ€์žฅ ์ƒ์œ„์— ์žˆ๋Š” ํƒ€์ž…


primitive type

  1. string, number, boolean
  2. symbol
  3. enum
  4. ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด

Object

  1. Array
  2. Tuple
  3. Functopn
  4. ์ƒ์„ฑ์ž <- ?
  5. Class
  6. Interface

Union

2๊ฐœ ์ด์ƒ์˜ ํƒ€์ž… ํ•˜๋‚˜์˜ ํƒ€์ž…์œผ๋กœ ์ง€์ •

1
const x: string | number;

intersection

๋‘ ํƒ€์ž…์„ ํ•ฉ์ณ ํ•˜๋‚˜์˜ ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ฆ

1
2
3
4
5
6
7
8
9
10
interface Dog {
leg: number;
}
interface Bird {
wing: number;
}
let dogBird: Dog & Brid = {
leg: number;
wing: number;
}

Primitive type

(์ƒ๋žต) string, number, boolean
symbol

์œ ์ผํ•˜๊ณ  ๋ถˆ๋ณ€์ ์ธ ์‹๋ณ„์ž (์ž์„ธํ•œ ์‚ฌํ•ญ)

ES6 ์ด์ƒ์—์„œ๋ถ€ํ„ฐ ์ง€์›

1
2
3
4
let test = Symbol(`test`);
let test2 = Symbol(`test`);

console.log(test === test2); // false

์‚ฌ์šฉ์ฒ˜

1
2
3
4
5
const RED = Symbol();
const YELLOW = Symbol();
const ORANGE = Symbol();

// ๊ทธ ์ž์ฒด๋กœ ์‹๋ณ„์ž ์—ญํ• ์„ ํ•จ

์‹ค์ œ ๊ฐœ๋ฐœ์ƒ์—์„œ ์‚ฌ์šฉํ•ด๋ณธ ์ ์ด ์—†์–ด์„œ ์•„์ง ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋Œ€ํ•œ ๋งํฌ

type

ํŠน์ • ๋ฌธ์ž์—ด๋งŒ ํ—ˆ์šฉํ•˜๋Š” ํƒ€์ž…

1
type EventType = "keyup" | "mouseover";
enum
1
2
3
enum Color { Red = 1, Green, Blue };
let color: Color.Green;
console.log(color); // 2

Object์™€์˜ ์ฐจ์ด์ ? (์—ฌ๊ธฐ์ฐธ๊ณ )

  1. object๋Š” ์†์„ฑ ์ž์œ ๋กญ๊ฒŒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ, enum์€ ๊ทธ๋ ‡์ง€ ์•Š์Œ
  2. enum์€ ํ•ญ์ƒ ๋ฆฌํ„ฐ๋Ÿด ํƒ€์ž… ์‚ฌ์šฉ
  3. enum์˜ ์†์„ฑ๊ฐ’์œผ๋กœ๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” ์ˆซ์ž๋งŒ ํ—ˆ์šฉ๋จ

์‚ฌ์šฉ ์ด์œ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const korean = `ko`
const english = `en`

// ์ฝ”๋“œ ์ค‘๋ณต
type Language = `ko` | `en`

// ์ฝ”๋“œ๊ธฐ ๊ธธ์–ด์ง
type Language = typeof korean | typeof english

const code: Language = korean

// enum ์‚ฌ์šฉ์‹œ ๊ฐ€๋…์„ฑ์ด ์ฆ๊ฐ€ํ•จ
enum Language {
korean = `ko`,
english = `en`
}

const code: Language = Language.korean

Object Type

Array
array type
1
let array; string[] = [`a`, `b`, `c`];
generic array type <>
1
let array: Array<string> = [`a`, `b`, `c`];

์ฐจ์ด์ : primitive ํƒ€์ž… ์™ธ์— object ํƒ€์ž…๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

1
let array: Array<() => string> = [() => `a`, () => `b`, () => `c`];
Tuple

n๊ฐœ์— ๋Œ€ํ•œ ๋ฐฐ์—ด ํƒ€์ž…

1
2
3
4
let array: [string, number] = [`text`, 10];

array = [1, `t`]; // error - ๊ฐ index์— ๋Œ€ํ•œ ํƒ€์ž…์ด ์•ˆ๋งž์Œ
array = [`t`, 10, `e`, 1]; // error - index๋ฅผ ์ดˆ๊ณผํ•˜์—ฌ ๋ฐ›์Œ

์ด๋ฒˆ ์žฅ์—์„œ ๋‹ค๋ฃจ์ง€ ์•Š๋Š” ๊ฒƒ๋“ค ์ƒ๋žต

(์ƒ๋žต) Function
(์ƒ๋žต) ์ƒ์„ฑ์ž
(์ƒ๋žต) Class
(์ƒ๋žต) Interface

๐Ÿ“p114 undefined !== null

undefined๋Š” ์„ ์–ธ์€ ๋ฌ์ง€๋งŒ, ๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š์€ ์ƒํƒœ

null์€ ์„ ์–ธ๊ณผ ๊ฐ’์ด ์—†๋‹ค๊ณ  ํ• ๋‹น๋œ ์ƒํƒœ

1
2
undefined === null; // false
undefined == null; // true

=== ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” type๊นŒ์ง€ ๊ฐ’์€์ง€ ์ฒดํฌํ•ด์ฃผ์ง€๋งŒ, ==๋Š” ๊ฐ’๋งŒ ์ฒดํฌ

ex.

1
2
1 == `1`;	// true
1 === `1`; // false

๐Ÿ“p123 for โ€ฆ in ๋ฌธ ์ฃผ์˜์‚ฌํ•ญ

์ด์ „์— ์ด์Šˆ ๊ฑธ๋ ธ๋˜ ์‚ฌํ•ญ์œผ๋กœ ์ž‘์„ฑ

for ... in ๋ฌธ์€ ์—†๋Š” ์ธ๋ฑ์Šค๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

ex.

1
2
3
for (i in [1,,,4]) {
console.log(i);
}

for...in๋ฌธ

for ... of๋ฌธ์€ ์ถœ๋ ฅ

for...of๋ฌธ